﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace NeoFramework.Net
{
    public class CookieJar
    {
        public Hashtable jar = new Hashtable();

        public Boolean hasCookies()
        {
            if (this.jar.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Cookie[] getCookies()
        {
            Cookie[] cookies = new Cookie[this.jar.Count];

            int i = 0;
            foreach (String key in this.jar.Keys)
            {
                cookies[i] = (Cookie)this.jar[key];
                i++;
            }

            return cookies;
        }

        public Boolean hasCookiesForDomain(String domain)
        {
            Boolean found = false;

            foreach (String key in this.jar.Keys)
            {
                Cookie tmpCookie = (Cookie)this.jar[key];

                if (tmpCookie.domain.Equals(domain, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                }
                else if (tmpCookie.domain.Equals(domain.Replace("www.", ""), StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                }
                else if (tmpCookie.domain.Equals(domain.Replace("www", ""), StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                }
            }

            return found;
        }

        public void parseCookiesRaw(String[] cookies, String domain)
        {
            foreach (String line in cookies)
            {
                String[] cookieParts = Regex.Split(line, ";");
                Cookie tmpCookie = new Cookie();

                foreach (String cookiePart in cookieParts)
                {
                    int eqIndex = cookiePart.IndexOf('=');

                    String key = cookiePart.Substring(0, eqIndex).Trim();
                    String value = cookiePart.Substring(eqIndex + 1).Trim();

                    if (key.Equals("expires", StringComparison.CurrentCultureIgnoreCase))
                    {
                        tmpCookie.expires = value;
                    }
                    else if (key.Equals("domain", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (value.Substring(0, 1) == ".")
                        {
                            value = value.Substring(1, value.Length - 1);
                        }

                        tmpCookie.domain = value;
                    }
                    else if (key.Equals("path", StringComparison.CurrentCultureIgnoreCase))
                    {
                        tmpCookie.path = value;
                    }
                    else
                    {
                        tmpCookie.key = key;
                        tmpCookie.value = value;
                    }
                }

                if (tmpCookie.domain.Length <= 0)
                {
                    tmpCookie.domain = domain;
                }

                this.jar[tmpCookie.key] = tmpCookie;
            }
        }

        public Cookie[] getCookiesByDomain(String domain)
        {
            ICollection<Cookie> retCookies = new List<Cookie>();
            ICollection<Cookie> remCookies = new List<Cookie>();

            foreach (String key in this.jar.Keys)
            {
                Cookie tmpCookie = (Cookie)this.jar[key];

                if (tmpCookie.hasExpired())
                {
                    remCookies.Add(tmpCookie);
                    continue;
                }

                if (tmpCookie.domain.Equals(domain, StringComparison.CurrentCultureIgnoreCase))
                {
                    retCookies.Add(tmpCookie);
                }
                else if (tmpCookie.domain.Equals(domain.Replace("www.", ""), StringComparison.CurrentCultureIgnoreCase))
                {
                    retCookies.Add(tmpCookie);
                }
                else if (tmpCookie.domain.Equals(domain.Replace("www", ""), StringComparison.CurrentCultureIgnoreCase))
                {
                    retCookies.Add(tmpCookie);
                }
            }

            foreach (Cookie tmpCookie in remCookies.ToArray<Cookie>())
            {
                this.jar.Remove(tmpCookie.key);
            }

            return retCookies.ToArray<Cookie>();
        }

        public String cookiesToString(Cookie[] cookies)
        {
            String cookieStr = "";

            foreach (Cookie cookie in cookies)
            {
                cookieStr += cookie.key + "=" + cookie.value + ";";
            }

            return cookieStr;
        }
    }
}
